package com.gopay.scheduler.service.cardauth.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.common.domain.cps.vo.FullCpsGenRecvOrder;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.service.cardauth.BankCardAuthService;

/**
 * 银行卡鉴权异常补偿
 * @BankCardAuthServiceImpl.java
 * @author dongdh
 * @2016年5月25日 下午3:57:41  www.gopay.com.cn Inc.All rights reserved.
 */
@Service("bankCardAuthService")
public class BankCardAuthServiceImpl implements BankCardAuthService {

    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;
    
    /**
     * 获取订单列表
     */
    @Override
    public List<FullCpsGenRecvOrder> getOrderList(String startDate, String endDate)  {
        
        List<FullCpsGenRecvOrder> orderList = new ArrayList<FullCpsGenRecvOrder>();

        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("r.gopay_order_id, ");
        sb.append("r.authentication_fee, ");
        sb.append("r.authentication_result, ");
        sb.append("t.main_recv_acct, ");
        sb.append("t.main_recv_cust_id ");
        sb.append(" from cps_gen_receipt_order r ");
        sb.append("left join  ");
        sb.append("CPS_GEN_MAIN_ORDER  t ");
        sb.append(" on t.gopay_order_id = r.gopay_order_id ");
        sb.append("where ");
        sb.append("t.created_ts  < to_timestamp(:endDate, 'yyyymmddHH24miss') ");
        sb.append("and t.created_ts > to_timestamp(:startDate, 'yyyymmddHH24miss') ");
        sb.append("and (r.authentication_result = '2221' or r.authentication_result = '3331') ");
        sb.append("and t.gopay_int_txn_cd = '00100' ");

        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startDate", startDate + "000000");
        map.put("endDate", endDate + "235959");
        
        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        
        FullCpsGenRecvOrder main;
        
        if (rsList != null && rsList.size() > 0) {
            for (Object[] arr : rsList) {
                main = new FullCpsGenRecvOrder();
                
                main.setGopayOrderId((String) arr[0]);
                main.setAuthenticationFee((BigDecimal) arr[1]);
                main.setAuthenticationResult((String) arr[2]);
                main.setMainRecvAcct((String) arr[3]);
                main.setMainRecvCustId((String) arr[4]);
                
                orderList.add(main);
            }
        }
        return orderList;
    }

}
